#include "gtest/gtest.h"
#include "chanlist.h"
#include "chanagents/defaultagent.h"
#include "components/board.h"

using namespace Wishmaster;

class TestChanList : public ::testing::Test
{
protected:
	virtual void SetUp()
	{
		chanlist = new ChanList();
	}

	virtual void TearDown()
	{
		delete chanlist;
	}

	void testManipulation();
	void testClear();

	void givenTwoChansAdded()
	{
		chan1 = new Chan("www.test1.com", NULL, NULL);
		chan2 = new Chan("www.test2.com", NULL, NULL);
		chanlist->addChan(chan1);
		chanlist->addChan(chan2);
	}

	void givenFreshChanlist()
	{

	}

	void whenChanlistIsCleared()
	{
		chanlist->clear();
	}

	void thenChanlistIsEmpty()
	{
		ASSERT_EQ(0, chanlist->chansCount());
	}

	void addNewBoard(Wishmaster::Chan* chan, const QString& boardName)
	{
		chan->addBoard(new Board(boardName, chan));
	}

	void addNewThread(Wishmaster::Board* board, const ThreadId& threadId)
	{
		board->visibleThreads()->push_back(ThreadPtr(new Thread(threadId, board)));
	}

	void addNewPost(Wishmaster::ThreadPtr thread, const PostId& postId)
	{
		thread->appendPost(PostPtr(new Post(postId)));
	}


	Wishmaster::ChanList* chanlist;
	Wishmaster::Chan* chan1;
	Wishmaster::Chan* chan2;
};

TEST_F(TestChanList, DefaultConstructor)
{
	givenFreshChanlist();
	thenChanlistIsEmpty();
}


TEST_F(TestChanList, ChansManipulation)
{
	givenTwoChansAdded();

	ASSERT_EQ(2, chanlist->chansCount());
	ASSERT_TRUE(chan1 == chanlist->getChan(0));
	ASSERT_TRUE(chan2 == chanlist->getChan(1));
}

TEST_F(TestChanList, Clear)
{
	givenTwoChansAdded();

	whenChanlistIsCleared();

	thenChanlistIsEmpty();
}

TEST_F(TestChanList, GetBoard)
{
	givenTwoChansAdded();
	Board* b = new Board("/b/", chan1);
	chan1->addBoard(b);
	ASSERT_TRUE(b == chanlist->getBoard("www.test1.com", "/b/"));
}

TEST_F(TestChanList, GetThread)
{
	ThreadId threadId("www.test1.com", "/b/", 123456);

	givenTwoChansAdded();
	addNewBoard(chan1, "/b/");
	addNewThread(chan1->board(0), threadId);

	ThreadPtr thread = chanlist->getThread(threadId);
	ASSERT_FALSE(!thread);
}

TEST_F(TestChanList, GetPost)
{
	ThreadId threadId("www.test1.com", "/b/", 123456);
	PostId postId(threadId, 123457);

	givenTwoChansAdded();
	addNewBoard(chan1, "/b/");
	addNewThread(chan1->board(0), threadId);
	addNewPost(chan1->board(0)->getThread(threadId), postId);

	PostPtr post = chanlist->getPost(postId);
	ASSERT_FALSE(!post);
}
